    #include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    qRegisterMetaType<QSharedPointer<BackupTask> >();
    qRegisterMetaType<QSharedPointer<RestoreTask> >();

    this->engine = 0;
    this->dbengine = new DBEngine();

    this->setMinimumSize(600, 600);
    QWidget *centralWidget = new QWidget();
    this->setCentralWidget(centralWidget);
    QHBoxLayout *centralLayout = new QHBoxLayout();
    centralWidget->setLayout(centralLayout);

    contentsWidget = new QListWidget();
    contentsWidget->setObjectName("contentsWidget");
    contentsWidget->setViewMode(QListView::IconMode);
    contentsWidget->setIconSize(QSize(96, 96));
    contentsWidget->setMovement(QListView::Static);
    contentsWidget->setMinimumWidth(122);
    contentsWidget->setMaximumWidth(122);
    contentsWidget->setMinimumHeight(this->width());
    contentsWidget->setMaximumHeight(this->width());
    centralLayout->addWidget(contentsWidget);

    pagesWidget = new QStackedWidget;
    centralLayout->addWidget(pagesWidget);

    pagesWidget->addWidget(newTaskWidget = new NewTaskWidget(this));
    pagesWidget->addWidget(backupProgressWidget = new BackupProgressWidget());
    pagesWidget->addWidget(taskListWidget = new TaskListWidget(this));
    pagesWidget->addWidget(settingsWidget = new SettingsWidget());

    createIcons();

    backupThread = new QThread(this);
    this->engine = new BackupEngine();
    this->engine->moveToThread(backupThread);
    this->engine = engine;
    connect(engine, &BackupEngine::progressMessageChanged, backupProgressWidget, &BackupProgressWidget::onProgressMessageChanged);
    connect(engine, &BackupEngine::progressMaximumChanged, backupProgressWidget, &BackupProgressWidget::onProgressMaximumChanged);
    connect(engine, &BackupEngine::progressValueAdd, backupProgressWidget, &BackupProgressWidget::onProgressValueAdd);
    connect(engine, &BackupEngine::newMessage, backupProgressWidget, &BackupProgressWidget::onNewMessage);
    connect(engine, &BackupEngine::changeCurrentTab, this, &MainWindow::changeCurrentTab);
    connect(engine, &BackupEngine::backupFinished, taskListWidget, &TaskListWidget::refreshTasks);

    connect(backupProgressWidget, &BackupProgressWidget::pauseButtonClicked, this, &MainWindow::toggleBackupEnginePause);
    connect(backupProgressWidget, &BackupProgressWidget::stopButtonClicked, this, &MainWindow::invokeBackupEngineStop);

    connect(newTaskWidget, &NewTaskWidget::newTaskCreated, taskListWidget, &TaskListWidget::refreshTasks);

    connect(taskListWidget, &TaskListWidget::taskBackupActivated, engine, &BackupEngine::performTask);
    connect(taskListWidget, &TaskListWidget::taskRestoreActivated, engine, &BackupEngine::performTask);

    backupThread->start();
    QTimer::singleShot(0, engine, SLOT(checkAvailableTasks()));
}

MainWindow::~MainWindow()
{
    backupThread->quit();
    backupThread->deleteLater();
}

void MainWindow::createIcons()
{
    singleBackupButton = new QListWidgetItem(contentsWidget);
    singleBackupButton->setIcon(QIcon(":/images/backup.png"));
    singleBackupButton->setSizeHint(QSize(120,120));
    singleBackupButton->setText("New task");
    singleBackupButton->setTextAlignment(Qt::AlignHCenter);
    singleBackupButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    backupProgressButton = new QListWidgetItem(contentsWidget);
    backupProgressButton->setIcon(QIcon(":/images/progress.png"));
    backupProgressButton->setSizeHint(QSize(120,120));
    backupProgressButton->setText("Backup Progress");
    backupProgressButton->setTextAlignment(Qt::AlignHCenter);

    planingButton = new QListWidgetItem(contentsWidget);
    planingButton->setIcon(QIcon(":/images/schedule.png"));
    planingButton->setSizeHint(QSize(120,120));
    planingButton->setText("Task list");
    planingButton->setTextAlignment(Qt::AlignHCenter);
    planingButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    settingsButton = new QListWidgetItem(contentsWidget);
    settingsButton->setIcon(QIcon(":/images/preferences.png"));
    settingsButton->setSizeHint(QSize(120,120));
    settingsButton->setText("Settings");
    settingsButton->setTextAlignment(Qt::AlignHCenter);
    settingsButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    connect(contentsWidget,
            SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
            this, SLOT(changePage(QListWidgetItem*,QListWidgetItem*)));
}

void MainWindow::changePage(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (!current)
        current = previous;

    pagesWidget->setCurrentIndex(contentsWidget->row(current));
}

void MainWindow::toggleBackupEnginePause()
{
    engine->toggleSuspend();
}

void MainWindow::invokeBackupEngineStop()
{
    engine->invokeStop();
}

void MainWindow::changeCurrentTab(quint8 index)
{
    if(index < pagesWidget->count()) {
        contentsWidget->setCurrentIndex(contentsWidget->model()->index(index,0));
    }
}

void MainWindow::onBackupFinished()
{
}
